﻿using DoNet.Common.Helpers;
using DoNet.Common.Result;
using DoNet.Extensions.Common;
using DoNet.Extensions.Mvc;
using DoNet.System.Dtos;
using DoNet.System.IServices;
using DoNet.System.Models;
using DoNet.System.Services;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using SqlSugar.Extensions;
using System.Configuration;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace DoNet.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TokenController : BaseApiController
    {
        //private readonly SignInManager<AppUser> signInManager;
        //private readonly UserManager<AppUser> userManager;
        //, SignInManager<AppUser> _signInManager, UserManager<AppUser> _userManager
        private readonly IConfiguration configuration;
        private readonly ILogger logger;
        private readonly IAppService appService;
        private readonly ITokenService tokenService;
        private readonly int accessExpire = 0;
        private readonly int refreshExpire = 0;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="iAPPService"></param>
        /// <param name="jwtModel"></param>
        public TokenController(ILogger<TokenController> _logger, ITokenService _tokenService, IConfiguration _configuration, IAppService _appService) : base(_logger, _tokenService)
        {
            configuration = _configuration;
            logger = _logger;
            appService = _appService;
            tokenService = _tokenService;
            //signInManager = _signInManager;
            //userManager = _userManager;
            accessExpire = configuration.app("TokenSettings:AccessExpire").ObjToInt();
            refreshExpire = configuration.app("TokenSettings:RefreshExpire").ObjToInt();
        }

        [HttpGet("get")]
        public async Task<IActionResult> GetTokenAsync(string appId, string appSecret)
        {
            CommonResult result = new CommonResult();
            var claims = new Dictionary<string, string>
            {
                { "uid", "1001" },
                { "role", "Admin" }
            };

            var model = appService.GetApp(appId);
            if (model != null)
            {
                if (appSecret == model.AppSecret)
                {
                    var tokenModel = tokenService.GetTokenPair(appId, appSecret, accessExpire, refreshExpire, claims,  , DateTime.Now);
                    if (tokenModel == null)
                    {

                    }
                    else
                    {

                    }
                }
                else
                {
                    //return ApiResultHelper.Error("AppSecret不正确！");
                    result.ErrCode = "40001";
                    result.ErrMsg = ErrCode.err40001;
                }
            }
            else
            {
                //return ApiResultHelper.Error("AppId不正确！");
                result.ErrCode = "40001";
                result.ErrMsg = ErrCode.err40001;
            }
            return Content(JsonHelper.SerializeObject(result));
        }

        [HttpGet("verify")]
        public async Task<IActionResult> Verify(string accessToken)
        {
            var isVerify = CustomTokenService.ValidateToken(accessToken);
            if (isVerify)
            {
                return ApiResultHelper.Success(CustomTokenService.ParseClaims(accessToken));
            }
            else
            {
                return ApiResultHelper.Error("无效的令牌");
            }
        }

        [HttpGet("refresh")]
        public async Task<IActionResult> Refresh(string refreshToken)
        {
            if (CustomTokenService.ValidateToken(refreshToken))
            {
                var result = CustomTokenService.ForceRefresh(refreshToken, accessExpire);
                if (result != null)
                {
                    return ApiResultHelper.Success(result);
                }
            }
            return ApiResultHelper.Error("无效的刷新令牌");
        }

    }
}
